import time

from motor.utils.logger import get_logger
from motor.utils.timer import Timer
from motor.controller.ft.strategy.strategy import StrategyBase
from motor.controller.ft.fault_manager import FaultManager

logger = get_logger(__name__)


class ScaleP2DStrategy(StrategyBase):
    def __init__(self) -> None:
        super().__init__()
        self.timer = Timer()
    
    def execute(self, id: int) -> None:
        while not self.event.is_set():
            # TODO: 30 seconds is a temporary value, we need to get it from the configuration.
            self.timer.start(callback=self.scale_p2d, timeout=30, args=(id,))
            time.sleep(5)
    
    def stop(self) -> None:
        self.timer.stop()
        self.event.set()
        with self._lock:
            self._is_finished = True
        logger.info(f"Stop Scale P2D strategy.")

    def scale_p2d(self, id: int) -> None:
        logger.info(f"Scale P2D strategy timeout, scale p2d.")
        # TODO: Get current config instance num and actual instance num.